package edu.xidian.sselab.cloudcourse.inputdata;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;


//"D:\\test.xlsx"
public class Excel_to_Object { //将excel中的文件转换为对应的类
    public ArrayList<Record> getObject(String filepath) throws IOException {

        FileInputStream fileInputStream = new FileInputStream(filepath); //输入对应的excel文件的路径
        XSSFWorkbook sheets = new XSSFWorkbook(fileInputStream); //选中对应的excel文件
        XSSFSheet sheet = sheets.getSheet("sheet1"); //选择表单1

        ArrayList<Record> records = new ArrayList<>();

        for(int i = 1;i < 3856;i ++){ //将每一条数据映射为Record类
            Record record = new Record();
            XSSFRow row = sheet.getRow(i); //循环获取每一行

            record.date = row.getCell(0).toString().split("\\.")[0]; //日期
            record.brand = row.getCell(1).toString(); //品牌
            record.type = row.getCell(2).toString(); //车型
            record.level = row.getCell(3).toString(); //级别
            record.nation = row.getCell(4).toString(); //国别
            record.salenumber = row.getCell(5).toString().split("\\.")[0]; //销量
            record.price = row.getCell(6).toString(); //价格

            records.add(record);
        }
        return records;
    }
    public static void main(String[] args) throws IOException {
        String filepath = "D:\\test.xlsx";
        Excel_to_Object eto = new Excel_to_Object();
        ArrayList<Record> objectlist = eto.getObject(filepath);
        for(Record record:objectlist)
            System.out.println(record);
    }
}
